package com.tgpms.web.earlierStage.service.impl;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.dao.DocSortTemplateMapper;
import com.tgpms.web.earlierStage.entity.DocSortTemplate;
import com.tgpms.web.earlierStage.service.EarlierDocSortTempService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class EarlierDocSortTempServiceImpl implements EarlierDocSortTempService {
	
	@Autowired
	private DocSortTemplateMapper docSortTemplateMapper;

	@Override
	public Result addDocSortTemp(DocSortTemplate docSortTemp, Result result) {
		docSortTemp.setSortId(Common.fromDateFormat("yyyyMMddHHmmssSSS"));
		//查询两个唯一键是否重复
		if(docSortTemplateMapper.selByUniqueKey(docSortTemp.getTaskId(), docSortTemp.getSortId())!=null) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT_REPETITION.getExplain());
			return result;
		}
		//主键形式 yyyyMMddHHmmss+UUID
		docSortTemp.setDstId(Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		docSortTemp.setRowstamp(docSortTemp.getDstId());
		docSortTemp.setInsertDate(new Date());
		int i = docSortTemplateMapper.insertSelective(docSortTemp);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(docSortTemp.getDstId());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@Override
	public Result updateDocSortTemp(DocSortTemplate docSortTemp, Result result) {
		docSortTemp.setModifyDate(new Date());
		int i = docSortTemplateMapper.updateByPrimaryKeySelective(docSortTemp);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}

	@Override
	public Result deleteDocSortTemp(String dstId, Result result) {
		int i = docSortTemplateMapper.deleteByPrimaryKey(dstId);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

}
